package com.vhall.component.framework.common.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

public class RequestHeadersUtil {

    /**
     * 判断是否是 mock 请求
     *
     * @param request
     * @return
     */
    public static boolean isGatewayDebug(HttpServletRequest request) {
        String debug = request.getHeader("x-gateway-debug");
        return debug != null;
    }

    /**
     * 判断是否是 feign request
     *
     * @param request
     * @return
     */
    public static boolean isFeignRequest(HttpServletRequest request) {
        String ug = request.getHeader("User-Agent");
        return Objects.nonNull(ug) && ug.contains("okhttp");
    }

    /**
     * 判断请求协议是否 https
     *
     * @param request
     * @return
     */
    public static boolean isSecureSchema(HttpServletRequest request) {
        // nginx 代理配置，获取协议信息
        String schema = request.getHeader("x-forwarded-proto");
        if (Objects.isNull(schema)) {
            schema = request.getScheme();
        }
        return "https".equalsIgnoreCase(schema);
    }

    /**
     * 是否是上传类服务
     *
     * @param request
     * @return
     */
    public static boolean isMultipart(HttpServletRequest request) {
        String contentType = request.getContentType();
        return contentType != null && contentType.toLowerCase().startsWith("multipart");
    }

}
